package com.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.show}")
    private Boolean swaggerEnabled;

    /**如果有多个包共在一个项目中的话，可以采用这种方式将API方法分开 明了*/

    @Bean
    public Docket createResAPi2(){
        return apiDocket("com.controller","示例接口");
    }


    private Docket apiDocket(String path,String name){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerEnabled)  /**是否启动swagger*/
                .select()
                .apis(RequestHandlerSelectors.basePackage(path))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .groupName(name);
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("co-project服务流水线示例项目服务接口")
                .description("示例项目文档说明")
                .version("1.0.0")
                .build();
    }

}
